//
//  HBWNetDataManager.m
//  HBW
//
//  Created by Mac on 16/3/16.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "HBWNetDataManager.h"
#import <AFHTTPSessionManager.h>


@interface HBWNetDataManager()

@property (nonatomic, strong) AFHTTPSessionManager  *sessionManager;
@end

@implementation HBWNetDataManager

static HBWNetDataManager *netMgr;
+ (instancetype)shareSessionManager{
    return [[self alloc] init];
    
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netMgr = [super allocWithZone:zone];
    });
    return netMgr;
}

- (instancetype)init{
    if (self = [super init]) {
        _sessionManager = [AFHTTPSessionManager manager];
//        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
        _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain", nil];

    }
    return self;
}

// token=d4ecb54a5a724b55&code=cqssc&format=json
- (void)homeLotterySuccess:(requestSuccess)success
                   failuer:(requestFaluier)failuer{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @"new_lottery";
    
    
    [self requestDataDaskWithParams:params
                             getAPI:LotteryDateAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}


/** 历史开奖 token=d4ecb54a5a724b55&code=cqssc&date=2016-03-29
 *
 */
- (void)HistoryLotteryWithParams:(NSMutableDictionary *)patams
                         Success:(requestSuccess)success
                         failuer:(requestFaluier)failuer{
    patams[@"page"] = @"history_lottery";
    [self requestDataDaskWithParams:patams
                             getAPI:LotteryDateAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}
//
//- (void)NewHistoryLotteryWithParams:(NSDictionary *)patams
//                         Success:(requestSuccess)success
//                         failuer:(requestFaluier)failuer{
//    
//    [self requestDataDaskWithParams:patams
//                             getAPI:LotteryDateAPI
//                            success:^(id responeseObj) {
//                                if (success) {
//                                    success(responeseObj);
//                                }
//                            } fauluer:^(NSError *error) {
//                                if (failuer) {
//                                    failuer(error);
//                                }
//                            } progress:nil];
//}


- (void)countResoureWithParam:(NSMutableDictionary *)param
                      Success:(requestSuccess)success
                      failuer:(requestFaluier)failuer{
    param[@"page"] = @"prediction";
    [self requestDataDaskWithParams:param
                             getAPI:CountResoureAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}


- (void)countResoureHistorySuccess:(requestSuccess)success
                           failuer:(requestFaluier)failuer{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"] = @"history";
    [self requestDataDaskWithParams:param
                             getAPI:CountResoureAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}

// 关于我们
- (void)aboutMeSuccess:(requestSuccess)success
               failuer:(requestFaluier)failuer{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @"about";
    [self requestDataDaskWithParams:params
                             getAPI:CountResoureAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}

// 反馈
- (void)feedbackWithParam:(NSMutableDictionary *)param
                  Success:(requestSuccess)success
                  failuer:(requestFaluier)failuer{
    param[@"page"] = @"feedback";
    [self requestDataDaskWithParams:param
                             getAPI:CountResoureAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}


- (void)advertImgWithParam:(NSMutableDictionary *)param
                   Success:(requestSuccess)success
                   failuer:(requestFaluier)failuer{
    param[@"page"] = @"advertising";
    [self requestDataDaskWithParams:param
                             getAPI:CountResoureAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}

//公告
- (void)affichetextWithParam:(NSMutableDictionary *)param
                   Success:(requestSuccess)success
                   failuer:(requestFaluier)failuer{
    if (!param) {
        param = [[NSMutableDictionary alloc] init];
    }
    param[@"page"] = @"placard";
    [self requestDataDaskWithParams:param
                             getAPI:AfficheAPI
                            success:^(id responeseObj) {
                                if (success) {
                                    success(responeseObj);
                                }
                            } fauluer:^(NSError *error) {
                                if (failuer) {
                                    failuer(error);
                                }
                            } progress:nil];
}

#pragma mark - 私有方法
// GET
- (void)requestDataDaskWithParams:(id)params
                           getAPI:(NSString *)api
                          success:(requestSuccess)success
                          fauluer:(requestFaluier)failuer
                     progress:(progress)progress
{
    
    [_sessionManager GET:api parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
//        NSLog(@"%@",HBWHostServer);
        if (progress) {
            progress(downloadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
//            NSLog(@"%@", responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failuer) {
            failuer(error);
        }
    }];
}

// POST
//- (void)POSTDataDaskWithParams:(id)params
//                      success:(requestSuccess)success
//                      fauluer:(requestFaluier)failuer
//                     progress:(progress)progress{
//    [_sessionManager POST:HBWHostServer parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
//        if (progress) {
//            progress(uploadProgress);
//        }
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (success) {
//            success(responseObject);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (failuer) {
//            failuer(error);
//        }
//    }];
//}

@end
